//
//  HWInfoRecommendListVC.swift
//  HWanGame
//
//  Created by Apple on 2024/8/12.
//  Copyright © 2024 94Wan. All rights reserved.
//

import UIKit
import JXSegmentedView

class HWInfoRecommendListVC: HWBaseViewController {

    var pageIndex = 1
    var cateId = 0
    var httpManager = HWHomeHttpManager()
    private lazy var tableView: UITableView = {
        let tableView = UITableView()
        tableView.delegate = self
        tableView.dataSource = self
        tableView.backgroundColor = .clear
        tableView.showsVerticalScrollIndicator = false
        tableView.rowHeight = 290*widthScale
        tableView.register(HWWuKongInfoCell.self)
        tableView.contentInsetAdjustmentBehavior = .never
        tableView.separatorStyle = .none
        return tableView
    }()
    
    var model:[HWHomeInfoItemModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .colorWithHexString(hexString: "#F7F8FA")
        view.addSubview(tableView)
        
        loadData()
        
        if #available(iOS 15.0, *) {
            self.tableView.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }

        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        addRefresh()
    }
    
    
    private func loadData() {
        
        httpManager.getHomeInfoList(class_id: cateId, p: pageIndex) { infoModel in
            self.tableView.mj_header?.endRefreshing()
            self.model = infoModel.infoData
            self.tableView.reloadData()
        } failure: { error in
            self.tableView.mj_header?.endRefreshing()
            print(error)
        }

    }
    
    private func addRefresh() {
        
        tableView.mj_header = MJRefreshNormalHeader()
        tableView.mj_header?.setRefreshingTarget(self, refreshingAction: #selector(self.loadNewData))
        tableView.mj_footer = MJRefreshFooter()
        tableView.mj_footer.setRefreshingTarget(self, refreshingAction: #selector(loadMoreData))
    }
    
    
    @objc private func loadNewData() -> () {
        self.pageIndex = 1
        loadData()
    }
    
    @objc private func loadMoreData(){
        self.pageIndex += 1
        loadData()
    }
}

//MARK: - UITableViewDelegate, UITableViewDataSource
extension HWInfoRecommendListVC: UITableViewDelegate, UITableViewDataSource {
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {

        return model?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "HWWuKongInfoCell", for: indexPath) as! HWWuKongInfoCell
        cell.infoModel = model?[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        let detailVC = HWHomeInfoDetailVC()
        detailVC.infoId = model?[indexPath.row].id ?? 0
        self.navigationController?.pushViewController(detailVC, animated: true)
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return UITableView.automaticDimension
    }
}

extension HWInfoRecommendListVC:JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
